cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

project(DiaryApp)

file(
    GLOB_RECURSE
    APP_SOURCES
    *.*
)

source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${APP_SOURCES})

# parse app version
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/app/version.c FILE_CONTENT)
# message(STATUS ${FILE_CONTENT})
string(REGEX MATCH "VERSION_MAJOR = ([0-9]*)" TEMP ${FILE_CONTENT})
set(APP_VERSION_MAJOR ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_MINOR = ([0-9]*)" TEMP ${FILE_CONTENT})
set(APP_VERSION_MINOR ${CMAKE_MATCH_1})
string(REGEX MATCH "VERSION_PATCH = ([0-9]*)" TEMP ${FILE_CONTENT})
set(APP_VERSION_PATCH ${CMAKE_MATCH_1})
unset(FILE_CONTENT)
unset(TEMP)

find_package(Git)
set(IN_TREE)
if (Git_FOUND)
    execute_process(
        COMMAND ${GIT_EXECUTABLE} rev-parse --is-inside-work-tree
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        RESULT_VARIABLE IN_TREE
        OUTPUT_QUIET
        ERROR_QUIET
    )
endif ()

if (IN_TREE EQUAL 0)
    message(STATUS "buildnumber using git commit count")
    execute_process(
        COMMAND ${GIT_EXECUTABLE} rev-list HEAD --count --no-merges
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE BUILD_NUMBER
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
elseif (EXISTS ${CMAKE_CURRENT_BINARY_DIR}/build_number.txt)
    file(READ ${CMAKE_CURRENT_BINARY_DIR}/build_number.txt BUILD_NUMBER)
    math(EXPR BUILD_NUMBER "${BUILD_NUMBER}+1")
else()
    set(BUILD_NUMBER 1)
endif()

message(STATUS "App Version: ${APP_VERSION_MAJOR}.${APP_VERSION_MINOR}.${APP_VERSION_PATCH}.${BUILD_NUMBER}")

configure_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/app/build_number.c.in
    ${CMAKE_CURRENT_BINARY_DIR}/app/build_number.c
)
list(APPEND APP_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/app/build_number.c)

# generator app.rc file
if (WIN32)
    set(ICON_PATH ${CMAKE_CURRENT_SOURCE_DIR}/app/app.ico)
    string(TIMESTAMP BUILD_DATE "%Y%m%d%H%M%S")

    configure_file(
        ${CMAKE_CURRENT_SOURCE_DIR}/app/app.rc.in
        ${CMAKE_CURRENT_BINARY_DIR}/app/app.rc
    )
    list(APPEND APP_SOURCES ${CMAKE_CURRENT_BINARY_DIR}/app/app.rc)
endif()

# 清除一些变量
unset(BUILD_DATE)
unset(APP_VERSION_MAJOR)
unset(APP_VERSION_MINOR)
unset(APP_VERSION_PATCH)

add_executable(DiaryApp WIN32 ${APP_SOURCES} $<TARGET_OBJECTS:CPR> $<TARGET_OBJECTS:SQLITE3> $<TARGET_OBJECTS:IMGUI> $<TARGET_OBJECTS:SQLITECPP>)
target_link_libraries(
  DiaryApp PRIVATE
  CURL::libcurl
  glfw
  OpenGL::GL
  ${FT2_LIBS}
  fmt::fmt
  Threads::Threads
  DiaryCore
  ${XLSWRITER_LIB}
  nng
)
target_include_directories(DiaryApp PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

if (MSVC)
    target_link_options(DiaryApp PRIVATE $<IF:$<CONFIG:Debug>,/SUBSYSTEM:CONSOLE,/SUBSYSTEM:WINDOWS> /ENTRY:mainCRTStartup)
endif()

# 需要X11
if (UNIX)
  find_package(X11 REQUIRED)
  target_link_libraries(DiaryApp PRIVATE X11::X11)
endif()

add_custom_target(
        COPY_APP_ASSETS ALL
        COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:DiaryApp>/assets"
        COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_FILE_DIR:DiaryApp>/extensions"
        COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/assets" "$<TARGET_FILE_DIR:DiaryApp>/assets"
        COMMAND ${CMAKE_COMMAND} -E copy_directory "${CMAKE_CURRENT_SOURCE_DIR}/builtin_scripts" "$<TARGET_FILE_DIR:DiaryApp>/extensions"
        COMMENT Copy DiaryApp Assets...
        DEPENDS DiaryApp
)

set_target_properties(DiaryApp COPY_APP_ASSETS PROPERTIES FOLDER "Application")

if (IN_TREE GREATER 0)
    add_custom_target(
        UpdateBuildNumber
        COMMAND ${CMAKE_COMMAND} -E echo "Current Build Number Is: ${BUILD_NUMBER}"
        COMMAND ${CMAKE_COMMAND} -E echo "${BUILD_NUMBER}" > "${CMAKE_CURRENT_BINARY_DIR}/build_number.txt"
        COMMENT "Update build number..."
    )
    set_property(TARGET UpdateBuildNumber PROPERTY FOLDER "Application")
endif ()
